package com.authine.cloudpivot.web.api.secondopen.controller;

import com.authine.cloudpivot.web.api.secondopen.domain.PartInfoDto;
import io.swagger.v3.core.util.Json;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import net.sf.json.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 远程调用WebService接口
 */
@RestController
@RequestMapping("/api/remoteCall")
public class RemoteCallController extends BaseController {

    @RequestMapping("/test")
    public String test(){
        return "test";
    }


    /**
     * 根据零件号获取零件信息
     */
    @PostMapping( "/getInfoByPart")
    public AjaxResult getInfoByPart(@RequestBody PartInfoDto partInfoDto) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://192.9.200.150:92/WebServiceATGMES.asmx?wsdl");
        Object[] objects = new Object[0];
        JSONArray jsonA=null;
        try {
            objects = client.invoke("GetDtBrPart",partInfoDto.getV_user(),partInfoDto.getV_corp(),partInfoDto.getV_domain(),partInfoDto.getV_pwd(),partInfoDto.getV_part());
            //按照各自的文档传入参数
            //objects = client.invoke("GetDtBrPart", "bpm", "atg", "atg", "Arn*1234","CA07069512A00000");
            //System.out.print("返回数据:" + JSONObject.toJSONString(objects[0]) );
            System.out.println("返回数据："+JSONArray.fromObject(objects));
            jsonA = JSONArray.fromObject(objects);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
            return AjaxResult.success(jsonA);
    }
}
